/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.framework.base.entity.SelectItem;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.entity.LotteryRecord;
import com.boxin.ims.modules.wechat.service.LotteryRecordService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 中奖人记录Controller
 * @author Jakemanse
 * @version 2013-09-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/lotteryRecord")
public class LotteryRecordController extends BaseController {

	@Autowired
	private LotteryRecordService lotteryRecordService;
	
	@ModelAttribute
	public LotteryRecord get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lotteryRecordService.get(id);
		}else{
			return new LotteryRecord();
		}
	}
	
	@RequiresPermissions("wechat:lotteryRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(@ModelAttribute("lotteryRecord") LotteryRecord lotteryRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			lotteryRecord.setUser(user);
		}
		if(lotteryRecord.getStartTime() == null){
			Date now = DateUtils.getDayStartTime();
			lotteryRecord.setCreateTime(now);
			lotteryRecord.setStartTime(now);
		}else{
			lotteryRecord.setCreateTime(lotteryRecord.getStartTime());
		}
        Page<LotteryRecord> page = lotteryRecordService.find(new Page<LotteryRecord>(request, response), lotteryRecord); 
        model.addAttribute("page", page);
        
        List<SelectItem> selectItems =  SelectItem.getCancelstatuslist();
        model.addAttribute("selectItems", selectItems);
        model.addAttribute("lotteryRecord", lotteryRecord);
        
		return "modules/wechat/lotteryRecordList";
	}
	
	@RequiresPermissions("wechat:lotteryRecord:view")
	@RequestMapping(value = "query")
	public String query(LotteryRecord lotteryRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			lotteryRecord.setUser(user);
		}
		Page<LotteryRecord> page = lotteryRecordService.find(new Page<LotteryRecord>(request, response), lotteryRecord); 
		model.addAttribute("page", page);
		
		List<SelectItem> selectItems =  SelectItem.getCancelstatuslist();
		model.addAttribute("selectItems", selectItems);
		
		return "modules/wechat/lotteryRecordList";
	}

	@RequiresPermissions("wechat:lotteryRecord:view")
	@RequestMapping(value = "form")
	public String form(LotteryRecord lotteryRecord, Model model) {
		model.addAttribute("lotteryRecord", lotteryRecord);
		return "modules/wechat/lotteryRecordForm";
	}

	@RequiresPermissions("wechat:lotteryRecord:edit")
	@RequestMapping(value = "save")
	public String save(LotteryRecord lotteryRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, lotteryRecord)){
			return form(lotteryRecord, model);
		}
		lotteryRecordService.save(lotteryRecord);
		addMessage(redirectAttributes, "保存中奖人记录'" + lotteryRecord.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/lotteryRecord/?repage";
	}
	
	@RequiresPermissions("wechat:lotteryRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		lotteryRecordService.delete(id);
		addMessage(redirectAttributes, "删除中奖人记录成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/lotteryRecord/?repage";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-1  上午8:10:50
	 * @function <p> 核销作废 </p>
	 * @param id
	 * @param request
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("wechat:lotteryRecord:edit")
	@RequestMapping(value = "update")
	public String updateStatus(Long id,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String status = request.getParameter("status");
		lotteryRecordService.updateStatusById(id, status) ;
		addMessage(redirectAttributes, ("1".equals(status)) ? "核销成功":"作废成功！");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/lotteryRecord/?repage";
	}

}
